package com.itheima.demo2;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

public class TcpServer {
    public static void main(String[] args) {
        //定义服务端套接字
        ServerSocket ss = null;
        try {
            ss = new ServerSocket(10086);
        } catch (IOException e) {
            //抛出异常
            e.printStackTrace();
        }

        System.out.println("服务器端显示:");

        while (true) {

            try {
                //监听接收客户端
                Socket accept = ss.accept();
                //读取客户端的字符流数据
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(accept.getInputStream()));

                //获取客户端IP
                String hostAddress = accept.getInetAddress().getHostAddress();
                //遍历流并打印
                String s;
               while((s = bufferedReader.readLine()) != null){
                   System.out.println(hostAddress+":"+s);
               }

               accept.shutdownInput();
            } catch (IOException e) {
                //抛出异常
                e.printStackTrace();
            }
        }


    }
}
